﻿
Imports System.IO
Imports System.Runtime.CompilerServices
Imports System.Net
Imports osi.root.connector
Imports osi.root.delegates
Imports osi.root.formation
Imports osi.root.procedure
Imports osi.root.utils

Public Module _question
    <Extension()> Public Function question(ByVal this As command,
                                           ByVal sr As Func(Of Byte(), pointer(Of Byte()), event_comb),
                                           ByVal response As pointer(Of command)) As event_comb
        Dim r As command = Nothing
        Dim ec As event_comb = Nothing
        Return New event_comb(Function() As Boolean
                                  r = New command()
                                  ec = question(this, sr, r)
                                  Return waitfor(ec) AndAlso
                                         goto_next()
                              End Function,
                              Function() As Boolean
                                  Return ec.end_result() AndAlso
                                         eva(response, r) AndAlso
                                         goto_end()
                              End Function)
    End Function

    <Extension()> Public Function question(ByVal this As command,
                                           ByVal sr As Func(Of Byte(), pointer(Of Byte()), event_comb),
                                           ByVal response As command) As event_comb
        Dim o As pointer(Of Byte()) = Nothing
        Dim ec As event_comb = Nothing
        Return New event_comb(Function() As Boolean
                                  If this Is Nothing OrElse
                                     sr Is Nothing OrElse
                                     response Is Nothing Then
                                      Return False
                                  Else
                                      o = New pointer(Of Byte())()
                                      ec = sr(this.to_bytes(), o)
                                      Return waitfor(ec) AndAlso
                                             goto_next()
                                  End If
                              End Function,
                              Function() As Boolean
                                  Return ec.end_result() AndAlso
                                         response.from_bytes(+o) AndAlso
                                         goto_end()
                              End Function)
    End Function

    <Extension()> Public Function question(ByVal this As command,
                                           ByVal sr As Func(Of String, 
                                                               pointer(Of HttpStatusCode), 
                                                               pointer(Of WebHeaderCollection), 
                                                               pointer(Of String), 
                                                               event_comb),
                                           ByVal response As pointer(Of command)) As event_comb
        Dim r As command = Nothing
        Dim ec As event_comb = Nothing
        Return New event_comb(Function() As Boolean
                                  r = New command()
                                  ec = question(this, sr, r)
                                  Return waitfor(ec) AndAlso
                                         goto_next()
                              End Function,
                              Function() As Boolean
                                  Return ec.end_result() AndAlso
                                         eva(response, r) AndAlso
                                         goto_end()
                              End Function)
    End Function

    <Extension()> Public Function question(ByVal this As command,
                                           ByVal sr As Func(Of String, 
                                                               pointer(Of HttpStatusCode), 
                                                               pointer(Of WebHeaderCollection), 
                                                               pointer(Of String), 
                                                               event_comb),
                                           ByVal response As command) As event_comb
        Dim ec As event_comb = Nothing
        Dim sc As pointer(Of HttpStatusCode) = Nothing
        Dim hc As pointer(Of WebHeaderCollection) = Nothing
        Dim r As pointer(Of String) = Nothing
        Return New event_comb(Function() As Boolean
                                  If this Is Nothing OrElse
                                     sr Is Nothing OrElse
                                     response Is Nothing Then
                                      Return False
                                  Else
                                      sc = New pointer(Of HttpStatusCode)()
                                      hc = New pointer(Of WebHeaderCollection)()
                                      r = New pointer(Of String)()
                                      ec = sr(this.to_uri(), sc, hc, r)
                                      Return waitfor(ec) AndAlso
                                             goto_next()
                                  End If
                              End Function,
                              Function() As Boolean
                                  Return ec.end_result() AndAlso
                                         (+sc) = HttpStatusCode.OK AndAlso
                                         Not (+hc) Is Nothing AndAlso
                                         strsame((+hc)(HttpResponseHeader.ContentType),
                                                 constants.http_responder_content_type,
                                                 strlen(constants.http_responder_content_type),
                                                 False) AndAlso
                                         response.from_uri(+r) AndAlso
                                         goto_end()
                              End Function)
    End Function

    <Extension()> Public Function question(ByVal this As command,
                                           ByVal sr As _func(Of Stream, 
                                                                Int64, 
                                                                pointer(Of HttpStatusCode), 
                                                                pointer(Of WebHeaderCollection), 
                                                                pointer(Of String), 
                                                                event_comb),
                                           ByVal response As command) As event_comb
        Return question(this,
                        Function(path_query As String,
                                 sc As pointer(Of HttpStatusCode),
                                 hc As pointer(Of WebHeaderCollection),
                                 result As pointer(Of String)) As event_comb
                            Dim ec As event_comb = Nothing
                            Return New event_comb(Function() As Boolean
                                                      If sr Is Nothing Then
                                                          Return False
                                                      Else
                                                          Dim ms As MemoryStream = Nothing
                                                          ms = New MemoryStream(str_bytes(path_query))
                                                          ec = sr(ms, ms.Length(), sc, hc, result)
                                                          Return waitfor(ec) AndAlso
                                                                 goto_next()
                                                      End If
                                                  End Function,
                                                  Function() As Boolean
                                                      Return ec.end_result() AndAlso
                                                             goto_end()
                                                  End Function)
                        End Function,
                        response)
    End Function

    <Extension()> Public Function question(ByVal this As command,
                                           ByVal sr As _func(Of Stream, 
                                                                Int64, 
                                                                pointer(Of HttpStatusCode), 
                                                                pointer(Of WebHeaderCollection), 
                                                                pointer(Of String), 
                                                                event_comb),
                                           ByVal response As pointer(Of command)) As event_comb
        Dim r As command = Nothing
        Dim ec As event_comb = Nothing
        Return New event_comb(Function() As Boolean
                                  r = New command()
                                  ec = question(this, sr, r)
                                  Return waitfor(ec) AndAlso
                                         goto_next()
                              End Function,
                              Function() As Boolean
                                  Return ec.end_result() AndAlso
                                         eva(response, r) AndAlso
                                         goto_end()
                              End Function)
    End Function
End Module
